import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:test_app/views/home/home.dart';
import 'package:test_app/views/user/userPage.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({super.key});

  @override
  indexScreen createState() => indexScreen();
}

class indexScreen extends State<IndexPage> {
  final PageController _pageController = PageController(initialPage: 0);
  int currentIndex = 0;
  final List<Widget> _pages = [HomePage(), userPage()];
  void onTapTab(int index) {
    debugPrint('携带参数: ${index.toString()}');
    setState(() => currentIndex = index);
    _pageController.animateToPage(
      index,
      duration: Duration(milliseconds: 300),
      curve: Curves.easeInOut,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Expanded(
            child: PageView(
              controller: _pageController,
              children: _pages,
              onPageChanged: (index) {
                setState(() {
                  currentIndex = index;
                });
              },
            ),
          ),
        ],
      ),
      bottomNavigationBar: _iconTextTypeTabBar3tabs(currentIndex, onTapTab),
    );
  }
}

Widget _iconTextTypeTabBar3tabs(currentIndex, onTapTab) {
  return TDBottomTabBar(
    currentIndex: currentIndex,
    TDBottomTabBarBasicType.iconText,
    useVerticalDivider: false,
    navigationTabs: [
      TDBottomTabBarTabConfig(
        tabText: '标签',
        selectedIcon: Icon(Icons.home),
        unselectedIcon: Icon(Icons.home),
        onTap: () => {onTapTab(0)},
      ),
      TDBottomTabBarTabConfig(
        tabText: '标签',
        selectedIcon: Icon(Icons.home),
        unselectedIcon: Icon(Icons.home),
        onTap: () => {onTapTab(1)},
      ),
    ],
  );
}
